<?php
class User_Model_OrderMapper {
	protected $_dbTable;

	public function __construct() {

	}

	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('User_Model_DbTable_Order');
		}
		return $this->_dbTable;
	}



	public function populateOrderFromArray($array, User_Model_Order $orderModel) {
		$orderModel->_oid = $array['oid'];
		$orderModel->_sid = $array['sid'];
		$orderModel->_uid = $array['uid'];
		$orderModel->_date = $array['date'];
		$orderModel->_type = $array['type'];
		$orderModel->_quantity = $array['quantity'];
		$orderModel->_status = $array['status'];
	}

	public function populateOrderFromRow(Zend_Db_Table_Row_Abstract $row, User_Model_Order $orderModel) {
		$orderModel->_oid = $row->oid;
		$orderModel->_sid = $row->sid;
		$orderModel->_uid = $row->uid;
		$orderModel->_date = $row->date;
		$orderModel->_type = $row->type;
		$orderModel->_quantity = $row->quantity;
		$orderModel->_status = $row->status;
	}


	public function save(User_Model_Order $orderInfo)
	{
		$oid = $orderInfo->_oid;
		$data = array(
			'oid' => $orderInfo->_oid,
            'sid'   => $orderInfo->_sid,
            'uid' => $orderInfo->_uid,
            'date' => $orderInfo->_date,
			'type' => $orderInfo->_type,
			'quantity' => $orderInfo->_quantity,
			'status' => $orderInfo->_status,
		);

		if (!isset($oid)) {
			unset($data['oid']);
			return $this->getDbTable()->insert($data);
		} else {
			return $this->getDbTable()->update($data, array('oid = ?' => $oid));
		}
	}

	public function find($oid, User_Model_Order $orderInfo)
	{
		$result = $this->getDbTable()->find($oid);
		if (0 == count($result)) {
			return false;
		}
		$row = $result->current();
		$this->populateOrderFromRow($row, $orderInfo);
		return true;
	}

	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new User_Model_Order();
			$this->populateOrderFromRow($row, $entry);
			$entries[] = $entry;
		}
		return $entries;
	}

	public function delete($oid)
	{
		$result = $this->getDbTable()->find($oid);
		if (0 == count($result)) {
			return false;
		}
		$this->getDbTable()->delete(array('oid = ?' => $oid));
		return true;
	}

	public function findSchedule(User_Model_Order $order, Admin_Model_Schedule $schedule) {
		$sid = $order->_sid;
		if (isset($sid)) {
			$scheduleMapper = new Admin_Model_ScheduleMapper();
			$hasS = $scheduleMapper->find($sid, $schedule);
			if (isset($hasS) && isset($schedule)) {
				return true;
			}
		}
		return false;
	}

	public function getOrders($uid) {
		$resultSet = $this->getDbTable()->fetchAll(array('uid = ?' => $uid));
		if (0 == count($resultSet)) {
			return;
		}
		$entries = array();
		foreach ($resultSet as $row) {
			$entry = new User_Model_Order();
			$this->populateOrderFromRow($row, $entry);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function getOrdersNotDone($uid) {
		$select = new Zend_Db_Table_Select($this->getDbTable());
		$select->where('uid = ?', $uid)
		->where('status = 0 OR status = 1');
		
		$resultSet = $this->getDbTable()->fetchAll($select);
		if (0 == count($resultSet)) {
			return;
		}
		$entries = array();
		foreach ($resultSet as $row) {
			$entry = new User_Model_Order();
			$this->populateOrderFromRow($row, $entry);
			$entries[] = $entry;
		}
		return $entries;
	}

	public function getOrdersByDate($date) {
		$resultSet = $this->getDbTable()->fetchAll(array('date = ?' => $date));
		if (0 == count($resultSet)) {
			return;
		}
		$entries = array();
		foreach ($resultSet as $row) {
			$entry = new User_Model_Order();
			$this->populateOrderFromRow($row, $entry);
			$entries[] = $entry;
		}
		return $entries;
	}
	
	public function getType($oid) {
		if (isset($oid)) {
			$orderInfo = new User_Model_Order();
			if ($this->find($oid, $orderInfo)) {
				$tid = $orderInfo->_type;
				if (isset($tid)) {
					$typeMapper = new User_Model_TypeMapper();
					$typeInfo = new User_Model_Type();
					if ($typeMapper->find($tid, $typeInfo)) {
						return $typeInfo;
					}
				}
			}
		}
	}
	
	public function updateStatus($oid, $status) {
		$data = array(
			'status' => $status,
		);
		
		$re = $this->getDbTable()->update($data, array('oid = ?' => $oid));
		return $re;
	}

	public function getMenu($oid) {
		if (isset($oid)) {
			$orderInfo = new User_Model_Order();
			if ($this->find($oid, $orderInfo)) {
				$date = new Zend_Date();
				$date->setDate($orderInfo->_date, DATE_FORMAT);
				$dayOfweek = $date->get(Zend_Date::WEEKDAY_DIGIT);
				$sid = $orderInfo->_sid;

				$schedule = new Admin_Model_Schedule();
				if ($this->findSchedule($orderInfo, $schedule)) {
					$mid = -1;
					switch ($dayOfweek) {
						case 0:
							$mid = $schedule->_sun;
							break;
						case 1:
							$mid = $schedule->_mon;
							break;
						case 2:
							$mid = $schedule->_tue;
							break;
						case 3:
							$mid = $schedule->_wed;
							break;
						case 4:
							$mid = $schedule->_thu;
							break;
						case 5:
							$mid = $schedule->_fri;
							break;
						case 6:
							$mid = $schedule->_sat;
							break;
					}
					if ($mid != -1) {
						$menuMapper = new Front_Model_MenuMapper();
						$menuInfo = new Front_Model_Menu();
						$has = $menuMapper->find($mid, $menuInfo);
						if (isset($has)) {
							return $menuInfo;
						}
					}
				}
			}
		}
	}
}